
package com.dps.omc.service.impl;
import com.dps.common.utils.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import com.dps.common.service.impl.AbstractPageService;
import com.dps.omc.dao.ILogFileDAO;
import com.dps.omc.model.LogFile;
import com.dps.omc.service.ILogFileService;

import java.io.ByteArrayInputStream;

@SuppressWarnings("unchecked")
@Service("LogFileServiceImpl")

public class LogFileServiceImpl extends AbstractPageService<ILogFileDAO,LogFile> implements ILogFileService {

	@Autowired
	private ILogFileDAO logFileDAO;
	@Autowired
	private GridFsTemplate gridFsTemplate;
	@Override
	public boolean getEnableDataPerm() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public ILogFileDAO getDao() {
		return logFileDAO;
	}

	@Override
	public String create(String taskId, String returnMsg) {

		String name = "tasklog"+taskId+".log";
		String path = "/log/"+IdGenerator.createNewId()+name;

		ByteArrayInputStream stream = new ByteArrayInputStream(returnMsg.getBytes());

		gridFsTemplate.store(stream, path/*, "image/png"/*, metaData*/);

		LogFile logFile = new LogFile();
		String id = IdGenerator.createNewId();
		logFile.setId(id);
		logFile.setName(name);
		logFile.setPath(path);
		logFile.setTaskId(taskId);
		this.add(logFile);
		return id;
	}
}
